Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
What is this structure in javascript return({object}={})=>{more code}?

I'm trying to understand what this code is doing, it's from a nodejs. I don't understand what is this structure is doing

return({object}={})=>{more code}

you can find the repository here: https://github.com/howardmann/clean-node/blob/master/models/teacher/teacher.js

let buildMakeTeacher = function(teacherValidator) {
  return ({
    name,
    subject,
    tenure = false
  } = {}) => {
    let {error} = teacherValidator({name, subject, tenure})
    if (error) throw new Error(error)

    return {
      getName: () => name,
      getSubject: () => subject,
      isTenure: () => tenure
    }
  }
}

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

In addition to answer by @Sarkar, the amount of curly braces here may shock you. The point of others (that don't declare a scope) is object destructuring. Breaking down:

let {error} = teacherValidator({name, subject, tenure})

This line takes object returned by teacherValidator and extracts a key named error from it. If it returned, say, {error: 'Fail', errorcode: 1}, then error is assigned to 'Fail'.

return ({
  name,
  subject,
  tenure = false
} = {}) => { doSomething(); }

This creates an anonymous function with complex argument handling. It is (by calling result, implementation should differ) equivalent to:

return function(params={}) {
  let {name, subject, tenure=false} = params;
  doSomething();
}

This function:

  • When called without args, has params set to empty object;
  • When called with argument, extracts name and subject keys from it (undefined if not present) and tenure key (false if missing).
  • Performs some action with these args (checks error and returns object with 3 anonymous functions like getters)
about 4 years ago · Juan Pablo Isaza Denunciar

0

It's a anonymous function

this

(object)=>{more code}

is equivalent to this

function functionName(object){
   moreCode
}

your code example is a function called buildMakeTeacher which is returning an anonymous function which returns an object

about 4 years ago · Juan Pablo Isaza Denunciar

0

Read about closure and curry a bit it will give you a better perspective.

But basically that means this function returns a new function which returns an object with callable actions.

So to call it you should do something like buildMakeTeacher(someValidstor)({name: “foo”, subject: “bar”}).getSubject()

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda